<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>vue3-escook</title>
  <link rel="stylesheet" href="./lib/style.css">
  <script type="importmap">
    {
      "imports" : {
        "vue": "./lib/vue-3.4.29.js"
      }
    }
  </script>
  <script type="module">
    import { createApp, ref, watchEffect, watch } from 'vue'

    const app = createApp({
      setup() {
        const title = ref('a')
        let unwatch

        const stop = () => {
          unwatch && unwatch()
          unwatch = null
        }

        const start = () => {
          // 防止开启多余的侦听器
          if (unwatch) return

          // unwatch = watchEffect(() => {
          //   document.title = title.value
          // })

          unwatch = watch(title, (newTitle) => {
            document.title = newTitle
          }, {
            immediate: true
          })
        }

        return {
          title,
          stop,
          start
        }
      }
    })

    app.mount('#app')
  </script>
</head>

<body>
  <div id="app">
    <input type="text" v-model="title">
    <hr>
    <button @click="start">开启侦听器</button>
    <button @click="stop">停止侦听器</button>
  </div>
</body>

</html>